﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using Runda.CMS.Common.Entity;
using Runda.CMS.Bll;

namespace Runda.CMS.Common.Exchange
{

    public enum MailType
    {
        /// <summary>
        /// 工作流
        /// </summary>
       WorkFlow,

        /// <summary>
        /// 消息提醒
        /// </summary>
       Notice
    }

    public class MailHandle
    {
        private static XmlDocument _Config;

        public static XmlDocument Config
        {
            get
            {
                if (_Config == null)
                {
                    _Config = new XmlDocument();
                    string xmlPath = AppDomain.CurrentDomain.BaseDirectory + @"Configuration\MailTemplate.xml";
                    _Config.Load(xmlPath);
                }
                return _Config;
            }
        }
    
        /// <summary>
        /// 发送用户密码找回邮件
        /// </summary>
        /// <param name="projectName"></param>
        /// <param name="newMember"></param>
        public static void SendEmailWorkFlow(string UserName, string LoginURL, string ReceiveMail, string Content)
        {
            XmlNode templateNode = Config.SelectSingleNode("*/MailTemplate[@Type='" + MailType.WorkFlow.ToString() + "']");
            if (templateNode == null) return;
            string to = ReceiveMail;
            string toDisplay = "Runda";
            string cc = string.Empty;
            string from = string.Empty;
            string fromDisplay = "";
            if (string.IsNullOrEmpty(to) || !IsValidEmail(to))
            {
                return;
            }
            string subject = templateNode["Subject"].InnerText;
            string body = templateNode["Body"].InnerText;

            subject = subject.Replace("{Sender}", UserName);
            body = body.Replace("{Content}", Content);

            MailHelper.SendEmail(to, toDisplay, cc, string.Empty, subject, body, null);
        }

        public static void SendEmailNotice(string UserName, string ReceiveMail, string Content)
        {
            XmlNode templateNode = Config.SelectSingleNode("*/MailTemplate[@Type='" + MailType.Notice.ToString() + "']");
            if (templateNode == null) return;
            string to = ReceiveMail;
            string toDisplay = "Runda";
            string cc = string.Empty;
            string from = string.Empty;
            string fromDisplay = "";
            if (string.IsNullOrEmpty(to) || !IsValidEmail(to))
            {
                return;
            }
            string subject = templateNode["Subject"].InnerText;
            string body = templateNode["Body"].InnerText;

            subject = subject.Replace("{Sender}", UserName);
            body = body.Replace("{Content}", Content);

            MailHelper.SendEmail(to, toDisplay, cc, string.Empty, subject, body, null);
        }
        public static bool IsValidEmail(string MailAddr)
        {
            return System.Text.RegularExpressions.Regex.IsMatch(MailAddr, @"^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$");
        }
    }
}
